/**
 *  获取token
 * @returns {string}
 */
function getTokenFromStorage() {
    let item = localStorage.getItem("doc-manager-token");
    if (!item) item = null;
    return item;
}

/**
 * 添加cookie
 * @param name
 * @param value
 * @param iDay
 */
function setCookie(name, value, iDay) {
    if (iDay) {
        let oDate = new Date();
        oDate.setDate(oDate.getDate() + iDay);
        document.cookie = name + '=' + encodeURIComponent(value) + ';expires=' + oDate;
    } else {
        document.cookie = name + '=' + encodeURIComponent(value);
    }
}

/**
 * 读取cookies
 * @param name
 * @returns {string|null}
 */
function getCookie(name) {
    let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

/**
 * 清除cookie
 * @param name
 */
function delCookie(name) {
    let exp = new Date();
    exp.setTime(exp.getTime() - 1);
    let cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

/**
 * 设置Token
 * @param token
 */
function setToken(token) {
    if (token)
        localStorage.setItem("doc-manager-token", token);
}

/**
 * Ajax全局设置
 */
$.ajaxSetup({
    async: false,
    type: 'POST',
    headers: {
        'token': getTokenFromStorage()
    },
    error: function (jaXhr, testState, errorMsg) {
        $.messager.alert('提示', '向服务器请求失败!', 'error');
    }
})
$.ajax();